$("#highLeverModal4Audit input").attr("readonly", true);
$("#highLeverModal4Audit select").attr("disabled", true);
$(function () {
    gMainDT = $("#mainDataTable").DataTable({
        ajax: {
            url: "../rds_HighLeverManage/getHighLever4DT",
            type: "POST",
            data: function (d) {
                d.search = $('#mainDataTable').DataTable().search(this.value);
                d.actorIds = gUserLoginResult.userActors;
                d.orgId = gUserLoginResult.orgId;
                d.audit = true;
                d.startRoad = "";
                d.endRoad = "";
                d.roadName = "";
                d.saveState = "";
            }
        },
        columns: [{
            data: "highLeverId",
            render: function (data, type, row, meta) {
                var checkId = "checkbox" + (parseInt(meta.row) + 1);
                return "<input type='checkbox' value='" + data + "' id='" + checkId + "'>";
            }
        }, {
            data: "highLeverId"
        }, {
            data: "highLeverPhoto",
            render: function (data, type, row) {
                return "<img src='" + data + "' style='width: 50px; height: 40px; cursor: pointer' " +
                    "onclick='$.showPicModal(\"" + data + "\")'>";
            }
        }, {
            data: null,
            render: function (data, type, row) {
                return row.highLeverProvince + " " + row.highLeverCity + " " + row.highLeverArea;
            }
        }, {
            data: "highLeverRoadName"
        }, {
            data: null,
            render: function (data, type, row) {
                return row.highLeverStartRoad + "——" + row.highLeverEndRoad;
            }
        }, {
            data: "highLeverOrgName"
        }, {
            data: "highLeverCreatorName"
        }, {
            data: "highLeverInstallTime"
        }, {
            data: "highLeverId",
            render: function (data, type, row) {
                var buttonOperation = "";
                var buttonAudit = "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.auditHighLever(" + data + ")' title='审核'>" +
                    "<i class='glyphicon glyphicon-check'></i></button> ";
                gCheck == 0 ? buttonOperation : buttonOperation += buttonAudit;

                buttonOperation += "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.showHighLever(" + JSON.stringify(row) + ")' title='查看'>" +
                    "<i class='glyphicon glyphicon-eye-open'></i></button> ";

                var buttonDelete = "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.deleteHighLever(" + data + ")' title='删除'>" +
                    "<i class='fa fa-minus-square'></i></button>";
                gDelete == 0 ? buttonOperation : buttonOperation += buttonDelete;

                return buttonOperation;
            }
        }],
        dom: "<'row'<'col-md-4'l><'#mainToolbar.col-md-8'>r>t<'row'<'col-md-5'i><'col-md-7'p>>",
        initComplete: function () {
            // 下拉工具栏按钮的权限
            $("#mainToolbar").empty();
            var strToolBar = "<div class='pull-right'><div class='btn-group'>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat'><i class='fa fa-cog'></i> 操作</button>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat dropdown-toggle' " +
                "data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>" +
                "<span class='caret'></span><span class='sr-only'>切换下拉</span></button>" +
                "<ul class='dropdown-menu'>" +
                "<li>" +
                "<button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='gMainDT.ajax.reload(null, false);'><i class='fa fa-refresh'></i> 刷新</button>" +
                "</li>";

            var buttonAudit = "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='$.auditHighLever(0)'><i class='fa fa-plus-square'></i> 审核</button></li>";
            gCheck == 0 ? strToolBar : strToolBar += buttonAudit;

            var buttonDelete = "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='$.deleteHighLever(0)'><i class='fa fa-minus-square'></i> 删除</button></li>";
            gDelete == 0 ? strToolBar : strToolBar += buttonDelete;

            $("#mainToolbar").append(strToolBar);
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        }
    });

});

$.fn.searchDT = function () {
    gMainDT.search().draw();
};

$.checkChange = function () {
    var isChecked = $("#batchCheck").prop('checked');
    $("input:checkbox[id^='checkbox']").prop("checked", isChecked);
};

$.auditHighLever = function (highLeverId) {
    var count = 1;
    if (highLeverId == 0) {
        count = $("input:checkbox[id^='checkbox']:checked").length;
        if (count == 0) {
            $.alert("请选择要审核的数据", 2);
            return;
        } else {
            highLeverId = "";
            $("input:checkbox[id^='checkbox']:checked").each(function () {
                highLeverId += $(this).val() + ",";
            });
            highLeverId = highLeverId.substring(0, highLeverId.length - 1);
        }
    }
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定审核通过该' + count + '条数据吗?</b>',
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: '../rds_HighLeverManage/auditHighLever',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        "highLeverIds": highLeverId
                    },
                    success: function (msg) {
                        if (msg != null && typeof (msg) != "undefined") {
                            if (msg.isSuccess == true) {
                                $.alert("数据审核通过成功", 1);
                                gMainDT.ajax.reload(null, false);
                            } else {
                                $.alert("数据审核通过失败", 2);
                            }
                        } else {
                            $.alert('系统错误，请联系管理员', 2);
                        }
                    },
                    error: function () {
                        $.alert('系统错误，请联系管理员', 2);
                    }
                });
            }
        }
    });
};

$.deleteHighLever = function (highLeverId) {
    var count = 1;
    if (highLeverId == 0) {
        count = $("input:checkbox[id^='checkbox']:checked").length;
        if (count == 0) {
            $.alert("请选择要删除的数据", 2);
            return;
        } else {
            highLeverId = "";
            $("input:checkbox[id^='checkbox']:checked").each(function () {
                highLeverId += $(this).val() + ",";
            });
            highLeverId = highLeverId.substring(0, highLeverId.length - 1);
        }
    }
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定删除该' + count + '条数据吗?</b>',
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: '../rds_HighLeverManage/deleteHighLever',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        "highLeverIds": highLeverId
                    },
                    success: function (msg) {
                        if (msg != null && typeof (msg) != "undefined") {
                            if (msg.isSuccess == true) {
                                $.alert("数据删除成功", 1);
                                gMainDT.ajax.reload(null, false);
                            } else {
                                $.alert("数据删除失败", 2);
                            }
                        } else {
                            $.alert('系统错误，请联系管理员', 2);
                        }
                    },
                    error: function () {
                        $.alert('系统错误，请联系管理员', 2);
                    }
                });
            }
        }
    });
};

$.showHighLever = function (jsonData) {
    $("#projectNumber").val(jsonData.projectNumber);
    $("#highLeverProvince").val(jsonData.highLeverProvince);
    $("#highLeverCity").val(jsonData.highLeverCity);
    $("#highLeverArea").val(jsonData.highLeverArea);
    $("#coordinate").val(jsonData.highLeverLongitude + "," + jsonData.highLeverLatitude);
    $("#highLeverRoadName").val(jsonData.highLeverRoadName);
    $("#highLeverStartRoad").val(jsonData.highLeverStartRoad);
    $("#highLeverEndRoad").val(jsonData.highLeverEndRoad);
    $("#highLeverBrand").val(jsonData.highLeverBrand);
    $("#highLeverInstallOrg").val(jsonData.highLeverInstallOrg);
    $("#highLeverInstallTime").val(jsonData.highLeverInstallTime);
    $("#highLeverRemark").val(jsonData.highLeverRemark);
    $("#highLeverOrgName").val(jsonData.highLeverOrgName);
    $("#highLeverPhotoImg").attr("src", jsonData.highLeverPhoto);
    $("#highLeverPeriod").val(jsonData.highLeverPeriod);
    $("#highLeverServiceLife").val(jsonData.highLeverServiceLife);
    $("#highLeverMainOrgName").val(jsonData.highLeverMainOrgName);
    $("#highLeverShape").val(jsonData.highLeverShape);
    $("#highLeverMaterial").val(jsonData.highLeverMaterial);
    $("#highLeverSize").val(jsonData.highLeverSize);
    $("#highLeverId").val(jsonData.highLeverId);
    $("#highLeverCreatorName").val(jsonData.highLeverCreatorName);
    $("#highLeverCreatorId").val(jsonData.highLeverCreatorId);
    $("#highLeverModifierId").val(gUserLoginResult.userId);
    $("#highLeverOrgId").val(jsonData.highLeverOrgId);
    $("#highLeverPhoto").val(jsonData.highLeverPhoto);


    $("#highLeverModal4Audit").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};

$.showAuditHighLever = function (highLeverId) {
    $.auditHighLever(highLeverId);
    $.clearModal($("#highLeverForm"), $("#highLeverModal4Audit"))
};



$("#myBtn").click(function () {
    $("#modalMapRoad").show();
    $("#startRoad").val($("#highLeverStartRoad").val());
    $("#endRoad").val($("#highLeverEndRoad").val());
    var url = "management/roadPoleMap.html";
    var coo = $("#coordinate").val(),
        coordinate,
        type = 1,
        x, y;
    if (coo != "") {
        coordinate = $("#coordinate").val().split(",");
        x = coordinate[0];
        y = coordinate[1];
    } else {
        x = $("#city").find("option:selected").text();
        y = $("#area").find("option:selected").text();
        type = 0;
    }
    url += "?x=" + x + "&y=" + y + "&z=" + type + "";//通过这种方式传参数
    $("#myMap").attr("src", url);
    $("#modal-map").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
});

$("#myBtn").click(function () {
    var coordinate = $("#coordinate").val().split(",");
    var x = coordinate[0];
    var y = coordinate[1];
    var url = "management/poleMap.html";
    url += "?x=" + x + "&y=" + y + "&z=1";//通过这种方式传参数
    $("#myMap").attr("src", url);
    $("#modal-map").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
});

